Skip to content

feat(server): add read-only MCP usage tools - #749

Open
tannerpolley wants to merge 1 commit into
mainfrom
cursor/mcp-usage-tools-1463
Open

tannerpolley wants to merge 1 commit into
mainfrom
cursor/mcp-usage-tools-1463

Conversation

@tannerpolley

Copy link
Copy Markdown
Owner

Problem

Outside agents connected through T3 Connect can read and steer threads, but the official MCP server has no way to read token, cost, or subscription-limit analytics. A bot that should suggest how to use tokens better has nothing to call.

Change

Add three read-only tools on the environment MCP server. Read only access is enough. They do not return prompts, messages, secrets, account emails, credential ids, or file paths.

  • t3_usage_summary reads the scan Usage already saved. It does not walk transcripts or resolve service tiers in the turn table. If that scan does not cover the requested 1–90 day window, the result is not_cached and no scan starts.
  • t3_usage_threads ranks recent threads and projects and lists threads near automatic compaction. The query is capped at the 40 most recently updated threads, 4 provider threads each, and the latest 8 turns, using the existing usage and ordinal indexes on a read-only connection.
  • t3_usage_limits returns the limits snapshot already in memory. It does not probe providers or redeem credits.

UsageService.readCachedSummary is the saved-scan path. A window the last complete scan did not cover returns without reading history.

Example t3_usage_summary result

Shape from the tool's fixture (not a live account). Reasoning is included in output. Cost is an API-equivalent estimate, not the subscription bill.

{
  "status": "ready",
  "window": { "sinceDay": "2026-11-22", "untilDay": "2026-11-28", "timeZone": "UTC", "days": 7 },
  "readAt": "2026-11-28T12:00:00.000Z",
  "refreshing": false,
  "pricing": { "status": "cached", "knownModels": 10 },
  "totals": {
    "tokens": {
      "uncachedInput": 1200,
      "cachedInput": 4000,
      "cacheWrite": 1000,
      "output": 600,
      "reasoning": 100,
      "total": 6800
    },
    "cacheHitRate": 0.6452,
    "costUsd": 1.75,
    "cacheSavingsUsd": 0.4,
    "costPerMillionTokens": 257.35,
    "categoryCostUsd": { "input": 0.2, "cacheRead": 0.1, "cacheWrite": 0.05, "output": 0.15, "other": 1.25 },
    "speedUsd": { "standard": 1.65, "fast": 0.1, "ultrafast": 0, "premium": 0.05 },
    "records": 3,
    "sessions": 3,
    "unpricedTokens": 0,
    "costSourceShare": { "modelPriced": 0.6667, "providerReported": 0.3333, "unpriced": 0 }
  },
  "byProvider": [{ "provider": "claude" }, { "provider": "codex" }],
  "byModel": [{ "provider": "claude", "model": "claude-opus" }, { "provider": "codex", "model": "gpt-test" }],
  "byDay": [
    { "day": "2026-11-27", "tokens": 300, "costUsd": 0.25 },
    { "day": "2026-11-28", "tokens": 6500, "costUsd": 1.5 }
  ]
}

t3_usage_threads adds threads, projects, and nearCompaction. Turn input includes cache reads and writes. Context nearAutoCompaction is at least 80% of the automatic-compaction point, or 85% of the max window when no compaction point is reported. t3_usage_limits returns accounts[] with windows (usedPercent, remainingPercent, resetsAt) and resetCredits.availableCount, without emails or credit ids.

Access and performance

MCP clients are approved for an access level, not a raw scope. These tools use the same read gate as listing projects and threads, so a Read only connection can call them and a write is impossible. The internal Usage RPC remains diagnostics:read; that scope is not granted to MCP clients.

The summary never starts the scan that has stalled long reads of the turn table. Thread rankings stay on indexed point reads (at most 40 × 4 × 8 rows) with a 2 second stop. Limits are the snapshot already held in memory.

Advertising the tools grows mcpToolListJsonBytes from 63967 to 65960. CI will reject that until a maintainer adds the agent-context-increase label.

Scope

Requested for the Token Optimizer bot on this fork: a fresh read-only usage tool, not a revival of any earlier budget report.

Verification

  • vp test run apps/server/src/mcp/toolkits/usage/handlers.test.ts — 8 passed. Covers totals, cache hit rate, cost per million, secret stripping, read-only access, not_cached, the saved-scan timeout, calendar windows, and the bounded thread query (latest 8 turns, local commands skipped, deleted and older threads left out, prompt text and workspace paths absent).
  • vp test run apps/server/src/usage/UsageService.test.ts -t "reads a covered window from the saved scan" — passed. A later transcript line is invisible to the cached read, and a wider uncovered window returns undefined.
  • effect-tsgo tsrs --noEmit on apps/server reports no errors in these files. Lint is clean on the new toolkit.

Rebuild or restart the server so /mcp serves the new tools, then reconnect the outside agent (or refresh its tool list). An existing Read only grant does not need a new approval. No database migration. If Usage has not saved a scan for the requested range, t3_usage_summary returns not_cached until that range is opened in Usage.

Model: Grok 4.7
Harness: Cursor

Open in Web Open in Cursor 

Outside agents could read threads but not token, cost, or limit analytics.
Add three read-only tools that use the saved usage scan, a bounded indexed
turn sample, and the in-memory limits snapshot.

Model: Grok 4.7
Harness: Cursor
Co-authored-by: Tanner Polley <tannerwpolley@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants